package org.dromara.work.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.io.unit.DataUnit;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.isoftstone.sign.SignGeneration;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.core.validate.QueryGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.work.domain.bo.SgjBeianReplySaveBo;
import org.dromara.work.domain.bo.SgjCheckReplySaveBo;
import org.dromara.work.domain.bo.SgjCheckSaveBo;
import org.dromara.work.domain.bo.SgjQueryBo;
import org.dromara.work.domain.vo.*;
import org.dromara.work.service.ISgjCheckService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 标准地名
 *
 * @author Chen
 * @date 2024-01-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/sgj")
public class SgjController extends BaseController {


    private final ISgjCheckService baseService;


    /**
     * 自定义分页查询
     */
    @SaIgnore
    @GetMapping("/page")
    public TableDataInfo<SgjBriefVo> page(@Validated(QueryGroup.class) SgjQueryBo bo, PageQuery pageQuery) {
        return baseService.customPageList(bo, pageQuery);
    }


    /**
     * 获取测试单表详细信息
     *
     * @param id 测试ID
     */
    @SaIgnore
    @GetMapping("/{id}")
    public R<SgjCheckVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable("id") String id) {
        return R.ok(baseService.queryById(id));
    }

    /**
     * 新增测试单表
     */
    @SaIgnore
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS, message = "{repeat.submit.message}")
    @PostMapping()
    public R<Void> add(@RequestBody SgjCheckSaveBo bo) {
        // 使用校验工具对标 @Validated(AddGroup.class) 注解
        // 用于在非 Controller 的地方校验对象
        ValidatorUtils.validate(bo, AddGroup.class);
        return toAjax(baseService.insertByBo(bo));
    }

    /**
     * 新增测试单表
     */
    @SaIgnore
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS, message = "{repeat.submit.message}")
    @PostMapping("/reply")
    public R<Void> addReply(@RequestBody SgjCheckReplySaveBo bo) {
        ValidatorUtils.validate(bo, AddGroup.class);
        return toAjax(baseService.insertReplyByBo(bo));
    }

    /**
     * 修改测试单表
     */
    @SaIgnore
    @Log(title = "测试单表", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SgjCheckSaveBo bo) {
        return toAjax(baseService.updateByBo(bo));
    }

    /**
     * 删除测试单表
     *
     * @param ids 测试ID串
     */
    @SaCheckPermission("demo:demo:remove")
    @Log(title = "测试单表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(baseService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 自定义分页查询
     */
    @GetMapping("/token")
    public String getToken() {
        String token="";
        String time= Calendar.getInstance().getTimeInMillis()+"";
        String md5= SecureUtil.md5(time+"123@Abc.com");
        return HttpUtil.get("http://172.27.247.36:8075/addrSearch/addrMatch/auth/getToken?user=ezsmzj&secret="+md5+"&time="+time);

    }

    @GetMapping("/sign")
    public R<String> getSign() {
        String sign="";
        try{
            Map<String,String> map=new HashMap<>();
            map.put("serviceId","ff808081928a8822019299860971768f");
            map.put("ak","56a8949ccbb0410686c6c3c3cc4e0708");
            map.put("appId","32A5D4C2F4D740369EBDA9CE5B2BA7B0");
            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
            String timestamp=sdf.format(new Date());
            map.put("timestamp", timestamp);
            sign= SignGeneration.generationSign(map,"fbada2aa42414c78");

        }catch (Exception ex){
            return R.fail(ex.getMessage());
        }
        return R.ok(sign);

    }



}
